<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="类型：1-普通职位，2-灵活用工，3-共享用工" v-bind="validateInfos.positionType">
	          <j-dict-select-tag v-model:value="formData.positionType" dictCode="position_type" placeholder="请选择类型：1-普通职位，2-灵活用工，3-共享用工" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否兼职：1-兼职，0-全职" v-bind="validateInfos.ifParttime">
            <j-dict-select-tag type='radio' v-model:value="formData.ifParttime" dictCode="if_parttime" placeholder="请选择是否兼职：1-兼职，0-全职" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公司ID" v-bind="validateInfos.companyId">
            <a-input v-model:value="formData.companyId" placeholder="请输入公司ID" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="名称ID" v-bind="validateInfos.postId">
            <a-input v-model:value="formData.postId" placeholder="请输入名称ID" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="职位名称" v-bind="validateInfos.postName">
            <a-input v-model:value="formData.postName" placeholder="请输入职位名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="职位描述" v-bind="validateInfos.descr">
	          <a-textarea v-model:value="formData.descr" rows="4" placeholder="请输入职位描述" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="地址名称" v-bind="validateInfos.addressName">
            <a-input v-model:value="formData.addressName" placeholder="请输入地址名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="门牌号" v-bind="validateInfos.addressHouse">
            <a-input v-model:value="formData.addressHouse" placeholder="请输入门牌号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="工作地点" v-bind="validateInfos.address">
            <a-input v-model:value="formData.address" placeholder="请输入工作地点" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="地点经度" v-bind="validateInfos.longitude">
            <a-input v-model:value="formData.longitude" placeholder="请输入地点经度" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="地点纬度" v-bind="validateInfos.latitude">
            <a-input v-model:value="formData.latitude" placeholder="请输入地点纬度" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="经验要求" v-bind="validateInfos.expRequire">
            <a-input v-model:value="formData.expRequire" placeholder="请输入经验要求" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="最低学历" v-bind="validateInfos.minEducation">
	          <j-dict-select-tag v-model:value="formData.minEducation" dictCode="education" placeholder="请选择最低学历" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="薪资范围" v-bind="validateInfos.salaryLow">
            <a-input v-model:value="formData.salaryLow" placeholder="请输入薪资范围" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="薪资范围" v-bind="validateInfos.salaryUp">
            <a-input v-model:value="formData.salaryUp" placeholder="请输入薪资范围" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="薪资月份" v-bind="validateInfos.salaryMonth">
	          <a-input-number v-model:value="formData.salaryMonth" placeholder="请输入薪资月份" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="薪资范围" v-bind="validateInfos.salary">
            <a-input v-model:value="formData.salary" placeholder="请输入薪资范围" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="薪资单位，兼职，元/时，元/天，元/周，元/月" v-bind="validateInfos.unit">
            <a-input v-model:value="formData.unit" placeholder="请输入薪资单位，兼职，元/时，元/天，元/周，元/月" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="兼职，薪资单价" v-bind="validateInfos.unitMoney">
	          <a-input-number v-model:value="formData.unitMoney" placeholder="请输入兼职，薪资单价" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="奖金绩效" v-bind="validateInfos.bonus">
            <a-input v-model:value="formData.bonus" placeholder="请输入奖金绩效" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="结算方式，兼职，日结、周结、月结、完工结" v-bind="validateInfos.settlementType">
	          <a-input-number v-model:value="formData.settlementType" placeholder="请输入结算方式，兼职，日结、周结、月结、完工结" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="招聘人数" v-bind="validateInfos.recruitsNumber">
	          <a-input-number v-model:value="formData.recruitsNumber" placeholder="请输入招聘人数" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="兼职工作日" v-bind="validateInfos.partDate">
            <a-input v-model:value="formData.partDate" placeholder="请输入兼职工作日" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="兼职时间" v-bind="validateInfos.partTime">
            <a-input v-model:value="formData.partTime" placeholder="请输入兼职时间" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="联系电话" v-bind="validateInfos.phone">
            <a-input v-model:value="formData.phone" placeholder="请输入联系电话" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="技能要求" v-bind="validateInfos.skill">
            <a-input v-model:value="formData.skill" placeholder="请输入技能要求" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="开始日期" v-bind="validateInfos.startDate">
		        <a-date-picker placeholder="请选择开始日期" v-model:value="formData.startDate" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="结束日期" v-bind="validateInfos.endDate">
		        <a-date-picker placeholder="请选择结束日期" v-model:value="formData.endDate" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="工作天数" v-bind="validateInfos.workDays">
	          <a-input-number v-model:value="formData.workDays" placeholder="请输入工作天数" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="共享人数" v-bind="validateInfos.shareNumber">
	          <a-input-number v-model:value="formData.shareNumber" placeholder="请输入共享人数" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="分享佣金" v-bind="validateInfos.shareMoney">
	          <a-input-number v-model:value="formData.shareMoney" placeholder="请输入分享佣金" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="浏览数量" v-bind="validateInfos.browseNumber">
	          <a-input-number v-model:value="formData.browseNumber" placeholder="请输入浏览数量" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" v-bind="validateInfos.remark">
            <a-input v-model:value="formData.remark" placeholder="请输入备注" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="县级城市名称" v-bind="validateInfos.city">
            <a-input v-model:value="formData.city" placeholder="请输入县级城市名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="县级城市编码" v-bind="validateInfos.citycode">
            <a-input v-model:value="formData.citycode" placeholder="请输入县级城市编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="市级城市名称" v-bind="validateInfos.pcity">
            <a-input v-model:value="formData.pcity" placeholder="请输入市级城市名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="市级城市编码" v-bind="validateInfos.pcitycode">
            <a-input v-model:value="formData.pcitycode" placeholder="请输入市级城市编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="tenantId" v-bind="validateInfos.tenantId">
	          <a-input-number v-model:value="formData.tenantId" placeholder="请输入tenantId" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="删除状态(0-正常,1-已删除)" v-bind="validateInfos.delFlag">
	          <a-input-number v-model:value="formData.delFlag" placeholder="请输入删除状态(0-正常,1-已删除)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="状态" v-bind="validateInfos.status">
            <a-input v-model:value="formData.status" placeholder="请输入状态" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import moment from 'moment';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../RmsCompanyPosition.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    disabled: { type: Boolean, default: false },
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    positionType: undefined,
    ifParttime: undefined,
    companyId: '',   
    postId: '',   
    postName: '',   
    descr: '',   
    addressName: '',   
    addressHouse: '',   
    address: '',   
    longitude: '',   
    latitude: '',   
    expRequire: '',   
    minEducation: '',   
    salaryLow: '',   
    salaryUp: '',   
    salaryMonth: undefined,
    salary: '',   
    unit: '',   
    unitMoney: undefined,
    bonus: '',   
    settlementType: undefined,
    recruitsNumber: undefined,
    partDate: '',   
    partTime: '',   
    phone: '',   
    skill: '',   
    startDate: '',   
    endDate: '',   
    workDays: undefined,
    shareNumber: undefined,
    shareMoney: undefined,
    browseNumber: undefined,
    remark: '',   
    city: '',   
    citycode: '',   
    pcity: '',   
    pcitycode: '',   
    tenantId: undefined,
    delFlag: undefined,
    status: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });
  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
